import java.util.TreeSet;

public class Solution02 {
	public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
		// TreeSet<Integer> set = new TreeSet<>();
		TreeSet<Long> set = new TreeSet<>();
		for (int i = 0; i < nums.length; i++) {
			Long floor = set.floor((long) nums[i]);
			if (floor != null && nums[i] - floor <= t) {
				return true;
			}
			Long ceil = set.ceiling((long) nums[i]);
			if (ceil != null && ceil - nums[i] <= t) {
				return true;
			}
			set.add((long) nums[i]);
			// 保持窗口大小 小于等于 k
			if (i >= k) {
				set.remove((long) nums[i - k]);
			}
		}
		return false;
	}
}
